/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistemadeleiloes.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import sistemadeleiloes.model.Usuario;

/**
 *
 * @author Pedro
 */
public class UsuarioDAO {

    public boolean inserir(Usuario u) throws SQLException {
        Connection conn = Database.getConnection();
        String sql = "INSERT INTO usuario VALUES (?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement pstm = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pstm.setString(1, u.getNome());
            pstm.setString(2, u.getEmail());
            pstm.setString(3, u.getCpf());
            pstm.setString(4, u.getRg());
            pstm.setString(5, u.getSenha());
            pstm.setString(6, u.getTelefone());
            pstm.setString(7, u.getEndereco());
            pstm.setInt(8, u.getPerfil());
            return (pstm.executeUpdate() != 0);
            
        } catch (SQLException e) {
            System.out.println("Erro ao inserir: " + e);
        }
        return false;
    }
    
    public Usuario pesquisar(int id) throws SQLException {
        Connection conn = Database.getConnection();
        String sql = "SELECT * FROM usuario WHERE id = ?";
        Usuario u = null;
        
        try{
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setLong(1, id);
            ResultSet rs = pstm.executeQuery();
            
            if (rs.next()){
                u = new Usuario();
                u.setId(rs.getInt("id"));
                u.setNome(rs.getString("nome"));
                u.setEmail(rs.getString("email"));
                u.setCpf(rs.getString("cpf"));
                u.setRg(rs.getString("rg"));
                u.setSenha(rs.getString("senha"));
                u.setTelefone(rs.getString("telefone"));
                u.setEndereco(rs.getString("endereco"));
                
                return u;
            }
        }catch(SQLException e){
            System.out.println("Erro ao listar: " + e);
        }
        
        return u;
    }
    
    public Usuario logar(String login, String senha) throws SQLException {
        Connection conn = Database.getConnection();
        String sql = "SELECT * FROM usuario WHERE email = ? AND senha = ?";
        Usuario u = null;
        
        try{
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setString(1, login);
            pstm.setString(2, senha);
            ResultSet rs = pstm.executeQuery();
            
            if (rs.next()){
                u = new Usuario(
                        rs.getString("nome"),
                        rs.getString("email"),
                        rs.getString("cpf"),
                        rs.getString("rg"),
                        rs.getString("senha"),
                        rs.getString("telefone"),
                        rs.getString("endereco"),
                        rs.getInt("perfil")
                        );
            }
            
            return u;
        } catch(SQLException e){
            System.out.println("Acesso inválido: " + e);
        }
        
        return u;
    }
}
